/**
 * Button 类的扩展
 */

import { Button, Color, EventHandler, Node } from 'cc';

export interface IBtnTransitionColors {
    normalColor: Readonly<Color>;
    pressedColor: Readonly<Color>;
    hoverColor: Readonly<Color>;
    disabledColor: Readonly<Color>;
}


declare module 'cc' {
    interface Button {
        /**
         * 设置点击回调（会清空点击回调数组，以保证只存在一个点击回调，即新增的回调）
         * @targetNode 挂在 compName 的节点
         * @compName 点击回调所属的组件的名字 
         * @handlerName 点击回调的名字
         * @returns 
         */
        setClickListener: (targetNode: Node, compName: string, handlerName: string) => Button;

        /**
         * 添加点击回调（可同时存在多个回调监听f）
         * @targetNode 挂在 compName 的节点
         * @compName 点击回调所属的组件的名字 
         * @handlerName 点击回调的名字
         * @returns 
         */
        addClickListener: (targetNode: Node, compName: string, handlerName: string) => Button;

        /**
         * 设置按钮过渡颜色
         * @param colors 按钮颜色配置
         */
        setTransitionColors(colors: IBtnTransitionColors): void;
    }
}

/**
 * 设置点击回调
 */
Button.prototype.setClickListener = function (targetNode: Node, compName: string, handlerName: string): Button {
    this.clickEvents.length = 0;
    this.addClickListener(targetNode, compName, handlerName);
    return this;
}

/**
 * 添加点击回调
 */
Button.prototype.addClickListener = function (targetNode: Node, compName: string, handlerName: string): Button {
    let eventHandler = new EventHandler();
    eventHandler.target = targetNode;
    eventHandler.component = compName;
    eventHandler.handler = handlerName;
    this.clickEvents.push(eventHandler);
    return this;
}

/**
 * 设置按钮过渡颜色
 */
Button.prototype.setTransitionColors = function (colors: IBtnTransitionColors) {
    this.normalColor = colors.normalColor;
    this.pressedColor = colors.pressedColor;
    this.hoverColor = colors.hoverColor;
    this.disabledColor = colors.disabledColor;
}